<template>
  <div class="app-container">
    <h1>汽车信息展示页面</h1>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆编号" prop="ipaddr">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车辆编号"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆VIN" prop="ipaddr">
        <el-input
          v-model="queryParams.carVin"
          placeholder="请输入车辆VIN"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆类型" prop="status">
        <el-select
          v-model="queryParams.carTid"
          placeholder="请选择车辆类型"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in typeList"
            :key="dict.tid"
            :label="dict.tname"
            :value="dict.tid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆状态" prop="status">
        <el-select
          v-model="queryParams.carSid"
          placeholder="请选择车辆状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in carStatus"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="电机厂商" prop="ipaddr">
        <el-input
          v-model="queryParams.djname"
          placeholder="请输入电机厂商"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池厂商" prop="ipaddr">
        <el-input
          v-model="queryParams.dcname"
          placeholder="请输入电池厂商"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="电机编号" prop="ipaddr">
        <el-input
          v-model="queryParams.carJnum"
          placeholder="请输入电机编号"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池编号" prop="ipaddr">
        <el-input
          v-model="queryParams.carCnum"
          placeholder="请输入电池编号"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button @click="thisAddCar" type="primary" style="margin-left: 16px;outline-width: 80px;line-height: 6px;margin-left: 10px">添加</el-button>
      </el-form-item>
    </el-form>
    <el-drawer
      title="车辆信息添加或修改"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="车辆编号" prop="ipaddr">
          <el-input
            v-model="queryParams.carNum"
            placeholder="请输入车辆编号"
            clearable
            style="width: 240px;"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车辆VIN" prop="ipaddr">
          <el-input
            v-model="queryParams.carVin"
            placeholder="请输入车辆VIN"
            clearable
            style="width: 240px;"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item><br>
        <el-form-item label="图片上传" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :headers="imgupload.headers"
            :action="imgupload.url + '?updateSupport=' + imgupload.updateSupport"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item><br>
        <el-form-item label="车辆类型" prop="status">
          <el-select
            v-model="queryParams.carTid"
            placeholder="请选择车辆类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in typeList"
              :key="dict.tid"
              :label="dict.tname"
              :value="dict.tid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="电子围栏" prop="status">
          <el-select
            v-model="queryParams.carWid"
            placeholder="请选择电子围栏"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车辆状态" prop="status">
          <el-select
            v-model="queryParams.carSid"
            placeholder="请选择车辆状态"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in carStatus"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="电机厂商" prop="status">
          <el-select
            v-model="queryParams.carJid"
            placeholder="请选择电机厂商"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in changList"
              :key="dict.csid"
              :label="dict.csname"
              :value="dict.csid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="电池厂商" prop="status">
          <el-select
            v-model="queryParams.carCid"
            placeholder="请选择电池厂商"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in changList"
              :key="dict.csid"
              :label="dict.csname"
              :value="dict.csid"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="电机编号" prop="ipaddr">
          <el-input
            v-model="queryParams.carJnum"
            placeholder="请输入电机编号"
            clearable
            style="width: 240px;"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="电池编号" prop="ipaddr">
          <el-input
            v-model="queryParams.carCnum"
            placeholder="请输入电池编号"
            clearable
            style="width: 240px;"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item><br>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="addOrUpdate">确定</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:logininfor:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-hasPermi="['system:logininfor:remove']"
        >清空</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="excel"
          v-hasPermi="['system:car:excel']"
        >导出excel</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:car:excelInto']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:logininfor:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆编号" align="center" prop="carNum" />
      <el-table-column label="车辆VIN" align="center" prop="carVin" :show-overflow-tooltip="true" />
      <el-table-column label="车辆类型" align="center" prop="tname" :formatter="statusFormat" />
      <el-table-column label="电子围栏" align="center" prop="carWid" />
      <el-table-column label="车辆图片" align="center" prop="carImg">
        <template #default="scope">
          <img :src="scope.row.carImg" alt="车辆图片" style="width: 50px; height: 50px;"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆状态" align="center" prop="carSid" >
        <template slot-scope="scope">
          <span v-if="scope.row.carSid === 1">在线</span>
          <span v-else-if="scope.row.carSid === 2">离线</span>
          <span v-else>未知状态</span>
        </template>
      </el-table-column>
      <el-table-column label="电机厂商" align="center" prop="djname" />
      <el-table-column label="电池厂商" align="center" prop="dcname" />
      <el-table-column label="电机编号" align="center" prop="carJnum" />
      <el-table-column label="电池编号" align="center" prop="carCnum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:car:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { list, cleanLogininfor } from "@/api/system/logininfor";
import { listCar, listType, listChang, addCar,delLogininfor ,excelOut} from '@/api/car/check'
import { getToken } from '@/utils/auth'

export default {
  name: "Logininfor",
  data() {
    return {
      imgupload: {
        //如果走gateway 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/car/uploadFile/"
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/car/importData"
      },
      carStatus:[
        { dictValue: 1, dictLabel: '离线' },
        { dictValue: 2, dictLabel: '在线' },
      ],
      //车辆编号
      myIstype:0,
      carNum:'',
      //车辆vin
      carVin:'',
      //车辆类型
      cartid:'',
      //车辆状态
      carstid:'',
      //车辆厂商
      carcsid:'',
      changList:[],
      typeList:[],
      drawer: false,
      direction: 'rtl',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 状态数据字典
      //statusOptions: [],
      // 日期范围
      //dateRange: [],
      // 查询参数
      queryParams: {
        carImg: '',
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        userName: undefined,
        status: undefined
      },
      imageUrl:""
    };
  },
  created() {
    //this.getList();
    //自己写的汽车信息展示页面
    this.carList();
    this.getDicts("sys_common_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    //minio图片上传
    beforeAvatarUpload(file) {
      // 可以在这里进行文件类型等的校验
      return true;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.queryParams.carImg=res;
      console.log("图片地址为："+JSON.stringify(res))
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    importTemplate() {
      this.download('agricultural/logistics/export', {
      }, `agricultural_${new Date().getTime()}.xlsx`)
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.transportId != undefined) {
            updateLogistics(this.form).then(response=>{
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导入按钮操作*/
    handleImport(){
      this.upload.title = "物流数据数据导入";
      this.upload.open = true;
    },
    excel(){
      //导出excel
      window.location.href= "http://localhost:10012/system/car/excel"
    },
    thisAddCar(){
      this.drawer = true
      this.myIstype=1
    },
    //自己写的添加或者修改方法
    addOrUpdate(){
        if (this.myIstype==1){
          alert("这是添加方法")
          //执行添加方法
          addCar(this.addDateRange(this.queryParams,this.dateRange)).then(res=>{
            if (res==true){
              alert("添加成功")
              this.myIstype=0
            }else {
              alert("添加失败")
              this.myIstype=0
            }
          })
        }else if (this.myIstype==2){
          //执行修改方法

        }
    },
    //添加或修改页面
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    //这是我写的汽车查询页面
    carList(){
      listCar(this.addDateRange(this.queryParams,this.dateRange)).then(response=>{
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      })
      listType().then(response=>{
        this.typeList=response;
        console.log(this.typeList)
      })
      listChang().then(response=>{
        this.changList=response;
      })
    },
    // /** 查询登录日志列表 */
    // getList() {
    //   this.loading = true;
    //   list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
    //       this.list = response.rows;
    //       this.total = response.total;
    //       this.loading = false;
    //     }
    //   );
    // },
    // // 登录状态字典翻译
    // statusFormat(row, column) {
    //   return this.selectDictLabel(this.statusOptions, row.status);
    // },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.carList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.infoId)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.infoId || this.ids;
      this.$confirm('是否确认删除此条数据？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          console.log("删除："+row.id)
          return delLogininfor(row.id);
        }).then(() => {
          this.carList();
          this.msgSuccess("删除成功");
        })
    },
    /** 清空按钮操作 */
    handleClean() {
        this.$confirm('是否确认清空所有登录日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return cleanLogininfor();
        }).then(() => {
          this.getList();
          this.msgSuccess("清空成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/logininfor/export', {
        ...this.queryParams
      }, `logininfor_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

